package com.demo.cms.filter;

import java.util.Map;

import javax.servlet.ServletContext;

import com.demo.core.model.Site;
import com.demo.webapp.cache.filter.SimpleSwitchPageCachingFilter;
import com.demo.webapp.config.Constants;
import com.demo.webapp.context.MultisiteContextHolder;

/**
 * 基于{@link SimpleSwitchPageCachingFilter} 的CMS页面缓存过滤器<br>
 * 
 * @author ryuu.kk
 *
 */
public class CmsSwitchPageCachingFilter extends SimpleSwitchPageCachingFilter {

	@SuppressWarnings("unchecked")
	@Override
	protected String[] urlMatchMetadata(ServletContext servletContext) {
		
		Site site = MultisiteContextHolder.getSite();
		Map<String, Object> config = (Map<String, Object>) servletContext.getAttribute(Constants.CONFIG);
		Map<String, Object> siteConfig = null;
		if (config != null) {
			siteConfig = ((Map<String, Object>) config.get(site.getDomain()));
		}
		return getAllowCacheUrl(siteConfig);
	}
	
	//use.webcache.url.allow
	@SuppressWarnings("unchecked")
	private String[] getAllowCacheUrl(Object obj) {
		
		// 站点的缓存开关
		boolean open = false;
		// 站点的WEB缓存匹配地址(使用正则或通配符)
		String[] cacheUrls = null;
		// 站点配置
		Map<String, Object> config = null;
		if (obj instanceof Map) {
			config = (Map<String, Object>) obj;
			open = (Boolean) config.get(Constants.APP_WEBCACHE_SWITCH);
		}
		
		if (open) {
			cacheUrls = (String[]) config.get(Constants.APP_WEBCACHE_URL);
		}
		if (LOG.isDebugEnabled()) {
			LOG.debug("Use Web-Cache ............. [" + open + "]");
		}
		return cacheUrls;
	}
}
